import { empty } from '@/utils/test'
import { getFaultType, setFaultType, removeFaultType } from '@/utils/auth'
import { statusOk, findByName, findById, nameById } from '../find'

// 装报修故障类型数组
const state = getFaultType()

const getters = {
	statusOk,
	findByName,
	findById,
	nameById,
	/**
	 * @example 通过产品类型ID来获取故障类型
	 * @param {Array} goodsType 产品类型ID
	 * @returns {Array} 产品类型
	 */
	findFaultByGoodsType: (state, getters) => (goodsType = []) => {
		const res = []
		if (empty(goodsType)) return res
		const statusOk = getters.statusOk
		for (const item of statusOk) {
			const { typeId } = item
			if (goodsType.includes(typeId)) res.push(item)
		}
		return res
	}
}

const mutations = {
	/**
	 * @example 功能：装报修故障类型
	 * @param {Object} data 装报修故障类型
	 * @returns {} 无
	 */
	SET_INIT: (state, data) => {
		if (!Array.isArray(data)) return console.warn('装报修故障类型', data)
		setFaultType(data)
		state.splice(0, Infinity, ...data)
	},
}

export default {
	namespaced: true,
	state,
	getters,
	mutations,
}